
package cn.com.pusai.core.page;

import java.util.ArrayList;
import java.util.Collection;

// Referenced classes of package cn.com.ecinc.core.page:
//			PageResult

public class DefaultPageResult
	implements PageResult
{

	private static final long serialVersionUID = 1L;
	private int currentPageNo;
	private int maxPageSize;
	private long totalCount;
	private int totalPageCount;
	private Collection items;

	public DefaultPageResult()
	{
		this(((Collection) (new ArrayList())), 0L, 0L, 0);
	}

	public DefaultPageResult(Collection items, long totalCount, long startIndex, int maxPageSize)
	{
		this.items = new ArrayList();
		this.items = ((Collection) (items != null ? items : ((Collection) (new ArrayList()))));
		this.totalCount = totalCount;
		if (this.totalCount == 0L)
		{
			currentPageNo = 0;
			this.maxPageSize = 0;
		} else
		{
			if (maxPageSize <= 0)
				maxPageSize = 10;
			this.maxPageSize = maxPageSize;
			currentPageNo = startIndexToPageNo(startIndex, maxPageSize);
		}
		totalPageCount = (int)(((totalCount + (long)maxPageSize) - 1L) / (long)maxPageSize);
	}

	public int getCurrentPageNo()
	{
		return currentPageNo;
	}

	public int getMaxPageSize()
	{
		return maxPageSize;
	}

	public long getTotalCount()
	{
		return totalCount;
	}

	public int getTotalPageCount()
	{
		return totalPageCount;
	}

	public Collection getItems()
	{
		return items;
	}

	public static int startIndexToPageNo(long startIndex, int maxPageSize)
	{
		if (maxPageSize <= 0)
			maxPageSize = 10;
		return (int)(startIndex % (long)maxPageSize <= 0L ? startIndex / (long)maxPageSize + 1L : startIndex / (long)maxPageSize + 2L);
	}

	public static long pageNoToStartIndex(long totalCount, int pageNo, int maxPageSize)
	{
		if (pageNo <= 1 || totalCount == 0L)
			return 0L;
		int startIndex = (pageNo - 1) * maxPageSize;
		if ((long)startIndex < totalCount)
			return (long)startIndex;
		else
			return ((totalCount - 1L) / (long)maxPageSize) * (long)maxPageSize;
	}

	public static int adjustPageNo(long totalCount, int pageNo, int maxPageSize)
	{
		return startIndexToPageNo(pageNoToStartIndex(totalCount, pageNo, maxPageSize), maxPageSize);
	}
}
